package com.umeox.watch.moto.dataservice.base;

import static android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import androidx.sqlite.db.SupportSQLiteDatabase;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.db.AppDataBase;
import com.umeox.watch.moto.dataservice.db.model.AlarmClock;
import com.umeox.watch.moto.dataservice.db.model.BuddyInfo;
import com.umeox.watch.moto.dataservice.db.model.ContactInfo;
import com.umeox.watch.moto.dataservice.db.model.DeviceCode;
import com.umeox.watch.moto.dataservice.db.model.DeviceConfig;
import com.umeox.watch.moto.dataservice.db.model.ForbidPeriod;
import com.umeox.watch.moto.dataservice.db.model.HeartRate;
import com.umeox.watch.moto.dataservice.db.model.HolderInfo;
import com.umeox.watch.moto.dataservice.db.model.IMGroup;
import com.umeox.watch.moto.dataservice.db.model.IMGroupMember;
import com.umeox.watch.moto.dataservice.db.model.LocationPeriod;
import com.umeox.watch.moto.dataservice.db.model.PrayerSetting;
import com.umeox.watch.moto.dataservice.db.model.QuranLearn;
import com.umeox.watch.moto.dataservice.db.model.ServerConfig;
import com.umeox.watch.moto.dataservice.db.model.StepInfo;
import com.umeox.watch.moto.dataservice.db.model.StoryPlayHistory;
import com.umeox.watch.moto.dataservice.db.model.TasksAttribute;
import com.umeox.watch.moto.dataservice.db.model.WallpaperInfo;


public final class BaseProvider extends ContentProvider {
    private static final String TAG = "DataCenter-BaseProvider";

    private static final int SERVERCONFIGPROVIDER_CONTENT_URI = 0;

    private static final int DEVICECONFIGPROVIDER_CONTENT_URI = 1;

    private static final int LOCATIONPERIODPROVIDER_CONTENT_URI = 2;

    private static final int ALARMPROVIDER_CONTENT_URI = 3;

    private static final int CONTACTPROVIDER_CONTENT_URI = 4;

    private static final int FOLLOWERPROVIDER_CONTENT_URI = 5;

    private static final int STEPINFOPROVIDER_CONTENT_URI = 6;

    private static final int WALLPAPERSPROVIDER_CONTENT_URI = 7;

    private static final int DEVICECODE_CONTENT_URI = 8;

    private static final int FORBIDPERIOD_CONTENT_URI = 9;

    private static final int HEARTRATE_CONTENT_URI = 10;

    private static final int HOLDER_INFO_URI = 11;

    private static final int PRAYER_SETTING_URI = 12;

    private static final int QURAN_LEARN_URI = 13;
    private static final int STORY_PLAY_HISTORY_URI = 14;
    private static final int IM_GROUP_URI = 15;
    private static final int TASKS_URI = 16;
    private static final int IM_GROUP_MEMBERS = 17;

    private final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    private String getTableName(int code) {
        switch (code) {
            case SERVERCONFIGPROVIDER_CONTENT_URI:
                return ServerConfig.TABLE_NAME;
            case DEVICECONFIGPROVIDER_CONTENT_URI:
                return DeviceConfig.TABLE_NAME;
            case LOCATIONPERIODPROVIDER_CONTENT_URI:
                return LocationPeriod.TABLE_NAME;
            case ALARMPROVIDER_CONTENT_URI:
                return AlarmClock.TABLE_NAME;
            case CONTACTPROVIDER_CONTENT_URI:
                return ContactInfo.TABLE_NAME;
            case FOLLOWERPROVIDER_CONTENT_URI:
                return BuddyInfo.TABLE_NAME;
            case STEPINFOPROVIDER_CONTENT_URI:
                return StepInfo.TABLE_NAME;
            case WALLPAPERSPROVIDER_CONTENT_URI:
                return WallpaperInfo.TABLE_NAME;
            case DEVICECODE_CONTENT_URI:
                return DeviceCode.TABLE_NAME;
            case FORBIDPERIOD_CONTENT_URI:
                return ForbidPeriod.TABLE_NAME;
            case HEARTRATE_CONTENT_URI:
                return HeartRate.TABLE_NAME;
            case HOLDER_INFO_URI:
                return HolderInfo.TABLE_NAME;
            case PRAYER_SETTING_URI:
                return PrayerSetting.TABLE_NAME;
            case QURAN_LEARN_URI:
                return QuranLearn.TABLE_NAME;
            case STORY_PLAY_HISTORY_URI:
                return StoryPlayHistory.TABLE_NAME;
            case IM_GROUP_URI:
                return IMGroup.TABLE_NAME;
            case TASKS_URI:
                return TasksAttribute.TABLE_NAME;
            case IM_GROUP_MEMBERS:
                return IMGroupMember.TABLE_NAME;


            default:
                throw new IllegalStateException("Unexpected value: " + code);
        }
    }

    @Override
    public final boolean onCreate() {
        MATCHER.addURI(BuildConfig.AUTHORITY, "ServerConfig", SERVERCONFIGPROVIDER_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "DeviceConfig", DEVICECONFIGPROVIDER_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "LocationPeriod", LOCATIONPERIODPROVIDER_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "Alarm", ALARMPROVIDER_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, ContactInfo.TABLE_NAME, CONTACTPROVIDER_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, BuddyInfo.TABLE_NAME, FOLLOWERPROVIDER_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, IMGroup.TABLE_NAME, IM_GROUP_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "StepInfo", STEPINFOPROVIDER_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "WallpaperInfo", WALLPAPERSPROVIDER_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "DeviceCode", DEVICECODE_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "ForbidPeriod", FORBIDPERIOD_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "HeartRate", HEARTRATE_CONTENT_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "HolderInfo", HOLDER_INFO_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "PrayerSetting", PRAYER_SETTING_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "QuranLearn", QURAN_LEARN_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "StoryPlayHistory", STORY_PLAY_HISTORY_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "Tasks", TASKS_URI);
        MATCHER.addURI(BuildConfig.AUTHORITY, "IMGroupMember", IM_GROUP_MEMBERS);
        return true;
    }

    @Override
    public final String getType(Uri uri) {
        String type;
        switch (MATCHER.match(uri)) {
            case SERVERCONFIGPROVIDER_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + ServerConfig.TABLE_NAME;
                break;
            }
            case DEVICECONFIGPROVIDER_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + DeviceConfig.TABLE_NAME;
                break;
            }
            case LOCATIONPERIODPROVIDER_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + LocationPeriod.TABLE_NAME;
                break;
            }
            case ALARMPROVIDER_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + AlarmClock.TABLE_NAME;
                break;
            }
            case CONTACTPROVIDER_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + ContactInfo.TABLE_NAME;
                break;
            }
            case FOLLOWERPROVIDER_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + BuddyInfo.TABLE_NAME;
                break;
            }
            case IM_GROUP_URI: {
                type = "vnd.android.cursor.dir/" + IMGroup.TABLE_NAME;
                break;
            }
            case STEPINFOPROVIDER_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + StepInfo.TABLE_NAME;
                break;
            }
            case WALLPAPERSPROVIDER_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + WallpaperInfo.TABLE_NAME;
                break;
            }
            case DEVICECODE_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + DeviceCode.TABLE_NAME;
                break;
            }
            case HEARTRATE_CONTENT_URI: {
                type = "vnd.android.cursor.dir/" + HeartRate.TABLE_NAME;
                break;
            }
            case HOLDER_INFO_URI: {
                type = "vnd.android.cursor.dir/" + HolderInfo.TABLE_NAME;
                break;
            }
            case PRAYER_SETTING_URI: {
                type = "vnd.android.cursor.dir/" + PrayerSetting.TABLE_NAME;
                break;
            }
            case QURAN_LEARN_URI: {
                type = "vnd.android.cursor.dir/" + QuranLearn.TABLE_NAME;
                break;
            }
            case STORY_PLAY_HISTORY_URI: {
                type = "vnd.android.cursor.dir/" + StoryPlayHistory.TABLE_NAME;
                break;
            }
            case FORBIDPERIOD_CONTENT_URI:
                return "vnd.android.cursor.dir/" + ForbidPeriod.TABLE_NAME;
            case TASKS_URI:
                return "vnd.android.cursor.dir/" + TasksAttribute.TABLE_NAME;
            case IM_GROUP_MEMBERS:
                return "vnd.android.cursor.dir/" + IMGroupMember.TABLE_NAME;
            default: {
                throw new IllegalArgumentException("Unknown URI" + uri);
            }
        }
        return type;
    }

    @Override
    public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                              String sortOrder) {
        final int code = MATCHER.match(uri);
        if (code < 0) {
            return null;
        }
        String sql = SQLiteQueryBuilder.buildQueryString(false
                , getTableName(code)
                , projection
                , selection
                , null
                , null
                , sortOrder
                , null);

        return AppDataBase.getInstance().query(sql, selectionArgs);
    }


    @Override
    public final Uri insert(Uri uri, ContentValues values) {
        final int code = MATCHER.match(uri);
        if (code < 0) {
            return null;
        }

        final AppDataBase dataBase = AppDataBase.getInstance();
        final SupportSQLiteDatabase writableDatabase = dataBase.getOpenHelper().getWritableDatabase();

        final long rowId = writableDatabase.insert(getTableName(code), CONFLICT_REPLACE, values);
        if (rowId >= 0) {
            getContext().getContentResolver().notifyChange(uri, null);
            return ContentUris.withAppendedId(uri, rowId);
        }
        return null;
    }


    @Override
    public final int delete(Uri uri, String selection, String[] selectionArgs) {
        final int code = MATCHER.match(uri);
        if (code < 0) {
            return 0;
        }
        final AppDataBase dataBase = AppDataBase.getInstance();
        final SupportSQLiteDatabase writableDatabase = dataBase.getOpenHelper().getWritableDatabase();
        final int count = writableDatabase.delete(getTableName(code), selection, selectionArgs);
        if (count > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
            Logger.d(TAG + ">>>notifyChange>>>" + uri);
        }
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        final int code = MATCHER.match(uri);
        if (code < 0) {
            return 0;
        }
        final AppDataBase dataBase = AppDataBase.getInstance();
        final SupportSQLiteDatabase writableDatabase = dataBase.getOpenHelper().getWritableDatabase();
        final int count = writableDatabase.update(getTableName(code), 0, values, selection, selectionArgs);
        if (count > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
            Logger.d(TAG + ">>>notifyChange>>>" + uri);
        }
        return count;
    }
}
